package cn.com.thingo.hyintelligent.ui.activity;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.DimmerInfo;
import com.th.opensdk.openapi.SwitchInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.base.BaseActivity;
import cn.com.thingo.hyintelligent.base.contract.LiteListContract;
import cn.com.thingo.hyintelligent.presenter.LiteListPresenter;
import cn.com.thingo.hyintelligent.ui.adapter.LiteListAdapter;
import cn.com.thingo.hyintelligent.ui.adapter.decoration.RecycleViewDivider;

/**
 * 灯光
 */
public class LiteListActivity extends BaseActivity<LiteListPresenter> implements LiteListContract.View {
    @BindView(R.id.toolbar_left)
    ImageView ivLeft;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.lite_list_rv)
    RecyclerView rvList;
    private List<DimmerInfo> dimmerInfoList = new ArrayList<>();
    private List<SwitchInfo> switchInfoList = new ArrayList<>();
    private LiteListAdapter adapter;
    private THOpenSDK thOpenSDK;
    private int currentRoomId;

    @Override
    protected int getLayout() {
        return R.layout.activity_lite_list;
    }

    @Override
    protected void initEventAndData() {
        thOpenSDK = THOpenSDK.getInstance();
        currentRoomId = getIntent().getIntExtra("current_room_id", -1);
        initView();
        initData();
    }

    private void initData() {
        List<DimmerInfo> dimmerList = thOpenSDK.getDimmerList();
        for (int i = 0; i < dimmerList.size(); i++) {
            DimmerInfo dimmerInfo = dimmerList.get(i);
            if (dimmerInfo.getRoomId() == currentRoomId) {
                dimmerInfoList.add(dimmerInfo);
            }
        }
        List<SwitchInfo> switchList = thOpenSDK.getSwitchList();
        for (int i = 0; i < switchList.size(); i++) {
            SwitchInfo switchInfo = switchList.get(i);
            if (switchInfo.getRoomId() == currentRoomId) {
                switchInfoList.add(switchInfo);
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        ivLeft.setImageResource(R.mipmap.back);
        tvTitle.setText(R.string.light);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new LiteListAdapter(switchInfoList, dimmerInfoList, mContext);
        rvList.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.gray_12)));
        rvList.setAdapter(adapter);

    }

    @OnClick({R.id.toolbar_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }


}
